﻿
@{
    ViewBag.Title = "搜索字段管理";
}
@section script{

    <script type="text/javascript">
        var index = 0;
        $(document).ready(function () {
            init();
        });

        function init() {
            loaddg();
            initDialog();
            initQueryFieldId();
            $('#div-dlg-dictionary').hide();
        }

        function loaddg() {
            index = 0;
            var params = $('#qryForm').serializeJSON();
            $('#dg').qwfDataGrid({
                url: '@Url.Content("~/QCRMSetting/QuickSreach/GetList")',
                title: '搜索字段管理',
                singleSelect: true,
                pagination: false,
                onDblClickRow: function (index, row) {
                    openEditWindows(row);
                },
                queryParams: params
            });
        }
        //初始化弹窗
        function initDialog() {
            $('#dlg').dialog({
                width: '80%',
                height: '80%',
                title: '搜索字段管理',
                iconCls: 'icon-save',
                closed: true,
                maximizable: true,
                modal: true,
                onResize: function () {
                    $(this).dialog('center');
                },
                buttons: [{
                    text: '保存',
                    iconCls: 'icon-ok',
                    handler: function () {
                        save();
                    }
                }
                ]
            });
        }
        ///打开弹窗
        function add() {
            $('#dlg').dialog('open');
            resetForm();
        }

        function edit() {
            var row = $('#dg').datagrid('getSelected');
            if (!row) {
                alertError('请选择要编辑的数据或双击要编辑的行。');
                return false;
            } else {
                openEditWindows(row);
            }
        }

        function openEditWindows(row) {
            if (!row) {
                alertError('请选择一条要编辑的数据。');
                return false;
            } else {
                console.log(row);
                resetForm();
                $('#dlg').dialog('open');
                $('#id').val(row.Id);

                $('#queryFieldId').combobox('setValue', row.QueryFieldId);
                $('#sreachTitle').textbox('setValue', row.SreachTitle);
                $('#sreachType').combobox('setValue', row.SreachType);
                if (row.SreachType == "select") {
                    $('#dictionaryId').combobox('setValue', row.DictionaryId);
                    $('#div-dlg-dictionary').show();
                } else {
                    $('#dictionaryId').combobox('setValue', '');
                    $('#div-dlg-dictionary').hide();
                }
            }
        }

        function del() {
            var row = $('#dg').datagrid('getSelected');
            if (!row) {
                alertError('请选择一条要删除的记录');
                return false;
            }
            $.messager.confirm('操作确认框', '确定要删除数据吗？删除数据会将用户的配置搜索条件一并删除,并且数据不可恢复。', function (r) {
                if (r) {
                    ajaxCore({
                        url: '@Url.Content("~/QCRMSetting/QuickSreach/Delete")',
                        data: {
                            id: row.Id
                        },
                        success: function (res) {
                            if (res.Success) {
                                loaddg();
                            }
                        }
                    });
                }
            });

        }

        function resetForm() {
            $('#dlg-form').form('clear');
            $('#dlg-form').form('reset');
        }

        function save() {
            var form = $('#dlg-form');
            var obj = form.serializeJSON();
            console.log(obj);

            if (obj.queryFieldId.isNull()) {
                alertError('请选择搜索字段');
                return false;
            } else if (obj.sreachTitle.isNull()) {
                alertError('请填写搜索标题');
                return false;
            } else if (obj.sreachType.isNull()) {
                alertError('请选择搜索类型');
                return false;
            } else if (!obj.sreachType.isNull() && obj.sreachType == 'select') {
                if (obj.dictionaryId.isNull() || obj.dictionaryId == '0') {
                    alertError('请选择字典');
                    return false;
                }
            }
            ajaxCore({
                url: '@Url.Content("~/QCRMSetting/QuickSreach/Save")',
                data: form.serialize(),
                success: function (res) {
                    if (res.Success) {
                        alertInfo('保存成功!');
                        loaddg();
                    }
                }
            });
        }

        function initQueryFieldId() {
            $('#queryFieldId').combobox({
                valueField: 'Id',
                textField: 'Name',
                url: '@Url.Content("~/QCRMSetting/QuickSreach/GetFieldList")',
                queryParams: {},
                onSelect: function (e) {
                    $('#sreachTitle').textbox('setValue', e.Name);
                }

            });
        }

        function onSelectType(e) {
            if (e.value == 'select') {
                $('#div-dlg-dictionary').show();
            } else {
                $('#div-dlg-dictionary').hide();
            }
        }

    </script>


}

<body>

    <form id="qryForm" name="qryForm" method="post">
        <div id="tb">
            <div class="qwf-searchbar" style="padding: 10px">
                <table width="100%" border="0" cellspacing="1" cellpadding="3">
                    <tr>
                        <td></td>
                    </tr>
                </table>
            </div>
            <div id="toolbar" style="padding: 3px">
                <a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-search'" style="width: 80px" onclick="loaddg();">查询</a>
                <a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-add'" style="width: 80px" onclick="add();">新增</a>
                <a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-edit'" style="width: 80px" onclick="edit();">编辑</a>
                <a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-remove'" style="width: 80px" onclick="del();;">删除</a>
            </div>
        </div>

        <table id="dg">
            <thead>
                <tr>
                    <th data-options="field:'Id',checkbox:true"></th>
                    <th field="SreachTitle" width="120">搜索名称</th>
                    <th field="SreachTypeName" width="150">搜索类型</th>
                    <th field="CreateTime" width="150">创建时间</th>
                    <th field="UpdateTime" width="150">修改时间</th>
                </tr>
            </thead>
        </table>

    </form>
</body>
<div id="dlg" style="max-width:650px;">

    <form id="dlg-form">
        <input type="hidden" id="id" name="id" />
        <div id="dlg-div" class="dlg-div">
            <div class="dlg-input-item">
                <select id="queryFieldId" name="queryFieldId" class="easyui-combobox" label="搜索字段<span class='font-red'>*</span>:" required="true"
                        data-options="labelPosition:'before',labelAlign:'left',labelWidth:'100px'"
                        style="width:300px;"></select>
            </div>

            <div class="dlg-input-item">
                <input class="easyui-textbox" id="sreachTitle" name="sreachTitle" label="搜索名称<span class='font-red'>*</span>：" required="true"
                       data-options="labelPosition:'before',labelAlign:'left',labelWidth:'100px'"
                       style="width:300px;">
            </div>

            <div class="dlg-input-item">
                <select id="sreachType" name="sreachType" class="easyui-combobox" label="搜索类型<span class='font-red'>*</span>:" required="true"
                        data-options="labelPosition:'before',labelAlign:'left',labelWidth:'100px', onSelect:function(e){ onSelectType(e);} "
                        style="width:300px;">
                    <option value="" selected>==请选择==</option>
                    @{
                        var sreachTypes = QWF.Framework.MainServices.CreateWebAppServices.GetConfigServices().GetNodeItemsByCode("qwf.qcrm.qicksreachtype");
                        foreach (var item in sreachTypes)
                        {
                            <option value="@item.Value">@item.Name</option>
                        }
                    }
                </select>
            </div>

            <div class="dlg-input-item" id="div-dlg-dictionary">
                <select id="dictionaryId" name="dictionaryId" class="easyui-combobox" label="所属字典:" required="true"
                        data-options="labelPosition:'before',labelAlign:'left',labelWidth:'100px'"
                        style="width:300px;">
                    <option value="" selected>==请选择==</option>
                    @{
                        var dics = (List<QWF.Framework.Web.BaseItemKeyValue>)ViewBag.DictionaryList;
                        foreach (var dic in dics)
                        {
                            <option value="@dic.Key">@dic.Value</option>
                        }
                    }
                </select>
            </div>

        </div>
    </form>

</div>
